<!-- 拖拽 Dialog -->
<template>
  <el-dialog
    v-bind="$attrs"
    :enableDrag="enableDrag"
    class="popup"
    :visible.sync="dialogShow"
    :fullscreen="fullscreen"
    :width="width"
    :height="height"
    :show-close="false"
    :append-to-body="appendToBody"
    v-el-drag-dialog
    @close="handleClose"
    ref="dragDialogRef"
  >
    <div
      class="popup-header"
      :class="{' has-title':title}"
      slot="title"
    >
      <div class="popup-title">{{title}}</div>
      <div class="popup-close-btn">
        <svg-icon
          className="fullscreen-icon"
          :icon-class="fullscreen ? 'fullscreen-dialog-exit' : 'fullscreen-dialog'"
          @click="handleFullscreen(fullscreen)"
        />
        <i
          class="el-icon-close"
          @click="handleClose"
        ></i>
      </div>
    </div>
    <div class="popup-box">
      <div
        class="slot-box"
        :style="{'max-height': !fullscreen ? (maxHeight+'px') : '','min-height': minHeight+'px'}"
      >
        <slot></slot>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import elDragDialog from "@localModules/ZhiXinLib/directive/el-drag-dialog";
import SvgIcon from '../SvgIcon/index.vue'
export default {
  name: "DragDialog",
  components:{
    SvgIcon
  },
  directives: {
    elDragDialog,
  },
  props: {
    show: {
      type: Boolean, // 是否弹出窗口
      required: true
    },
    enableDrag: {
      type: Boolean, // 是否开启拖拽功能
      default: true
    },
    title: {
      type: String // 弹出的标题
    },
    width: {
      type: String, // 弹出的宽度
      default: "80%"
    },
    appendToBody: {
      type: Boolean, // 是否加在body上  多层弹出嵌套勾选
      default: false
    },
    height: String,
    maxHeight: {
      // 弹窗框最大高度
      type: Number,
      default: document.documentElement.clientHeight - 140 // 导航栏-Dialog标题栏-底部(50-40-50)
    },
    minHeight: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      dialogShow: this.show,
      fullscreen: false,
    };
  },
  watch: {
    show(val) {
      this.dialogShow = val;
    },
    dialogShow(val) {
      this.$emit("update:show", val);
    }
  },
  methods: {
    handleClose() {
      this.close();
      this.$emit("close");
    },
    handleFullscreen(fullscreen) {
      this.fullscreen = !fullscreen
      if (this.fullscreen) {
        const el = this.$refs.dragDialogRef.$el
        const dragDom = el.querySelector('.el-dialog')
        if (dragDom) {
          dragDom.style.top = 0;
          dragDom.style.left = 0;
        }
        this.$emit("fullscreen")
      }
    },
    close() {
      this.dialogShow = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.popup {
  overflow: hidden;
  & ::v-deep .el-dialog {
    width: 100%;
    margin: 0 auto;
    overflow: auto;
  }
  & ::v-deep .el-dialog__body {
    padding: 0;
  }
  & ::v-deep .el-dialog__header {
    padding: 0;
  }
  & ::v-deep .el-dialog--center {
    padding: 0;
  }
}

.popup-header {
  width: 100%;
  z-index: 1001;
  display: flex;
  align-items: center;
  padding: 10px 0;
  .popup-title {
    flex: 1;
    font-size: 14px;
    font-weight: bold;
    line-height: 14px;
    padding: 0 20px;
  }
  .popup-close-btn {
    cursor: pointer;
    padding: 4px 20px;
    border-radius: 3px;
    color: #909399;
    font-size: 20px;
    line-height: 14px;
  }
}

.has-title {
  background-color: #f2f3f6;
}

.popup-box {
  background-color: #fff;
  .slot-box {
    padding: 15px;
    overflow: auto;
  }
}
</style>
